﻿using System;
using System.Linq;
using System.Xml.Serialization;

namespace CXW.Core.Util
{
    [Serializable]
    [XmlRoot(Namespace = "", IsNullable = false)]
    public class ViewDatas
    {
        /// <remarks/>
        [XmlElement("ViewData")]
        public ViewData[] ArrayViewData;

        /// <summary>单据表头</summary>
        public ViewData Header
        {
            get
            {
                if (ArrayViewData != null && ArrayViewData.Length > 0)
                    return ArrayViewData[0];
                return null;
            }
        }
        /// <summary>
        /// 单据明细
        /// </summary>
        public ViewData Detail
        {
            get
            {
                if (ArrayViewData != null && ArrayViewData.Length > 1)
                    return ArrayViewData[1];
                return null;
            }
        }

        public ViewData this[string dataKey]
        {
            get
            {
                return
                    ArrayViewData.FirstOrDefault(
                        item =>
                            (!string.IsNullOrEmpty(item.DataKey) &&
                             item.DataKey.Equals(dataKey, StringComparison.OrdinalIgnoreCase)));
            }
        }
    }

    public class ViewData
    {
        /// <summary>
        /// 查询结果集 是否 IList&lt;T&gt; 结构
        /// 默认返回的 DataTable
        /// </summary>
        [XmlAttribute()]
        public bool IsList;

        /// <remarks/>
        public ViewDatasViewDataQuery Query;

        [XmlAttribute()]
        public string ParentKey;

        public ViewDatasViewDataAdd Add;
        public ViewDatasViewDataUpdate Update;
        public ViewDatasViewDataDelete Delete;
        public ViewDatasViewDataVerify Verify;

        [XmlArrayItem("ExtQuery", IsNullable = false)]
        public ViewDatasViewDataExtQuery[] ExtQuerys;

        /// <summary>功能说明</summary>
        [XmlAttribute()]
        public string Caption;

        [XmlAttribute()]
        public string XmlConfig;

        /// <summary>
        /// 用于删除单据（借用审核xml文件）
        /// </summary>
        [XmlAttribute()]
        public string CfgConfig;

        [XmlAttribute()]
        public string DataKey;

        ViewTemplate _template;
        public ViewTemplate Template
        {
            get
            {
                if (string.IsNullOrEmpty(XmlConfig))
                {
                    MessageService.ShowMessage("没有指定配置文件。");
                    return null;
                }
                return _template == null ? _template = ConfigManager.GetViewTemplate(XmlConfig) : _template;
            }
        }
    }

    /// <remarks/>
    public class ViewDatasViewDataQuery : IViewDatasViewData
    {
        private string assKey = "Common";
        private string cls = "DynamicQuery";

        /// <summary>数据访问层 dll文件 对应的 key</summary>
        [XmlAttribute()]
        public string AssemblyKey
        {
            set { assKey = value; }
            get { return assKey; }
        }
        /// <summary>数据访问层 类名</summary>
        [XmlAttribute()]
        public string ClassName
        {
            set { cls = value; }
            get { return cls; }
        }
        /// <summary>存储名称</summary>
        [XmlAttribute()]
        public string ProcName { set; get; }
    }

    public interface IViewDatasViewData
    {
        /// <summary>数据访问层 dll文件 对应的 key</summary>
        string AssemblyKey { set; get; }
        /// <summary>数据访问层 类名</summary>
        string ClassName { set; get; }
        /// <summary>存储名称</summary>
        string ProcName { set; get; }
    }

    public class ViewDatasViewDataAdd : ViewDatasViewDataQuery
    {
    }
    public class ViewDatasViewDataUpdate : ViewDatasViewDataQuery
    {
    }
    public class ViewDatasViewDataDelete : ViewDatasViewDataQuery
    {
    }
    public class ViewDatasViewDataVerify : ViewDatasViewDataQuery
    {
    }

    public class ViewDatasViewDataExtQuery
    {
        private string name;

        [XmlAttribute()]
        public string Name
        {
            set { name = value; }
            get
            {
                if (string.IsNullOrEmpty(name))
                    return CtrlName;
                return name;
            }
        }

        [XmlAttribute()]
        public string CtrlName;

        [XmlAttribute()]
        public string RelatCtrlName;

        [XmlAttribute()]
        public string DataField;
        /// <summary>
        /// Treeview
        /// </summary>
        [XmlAttribute()]
        public string ParentField;

        [XmlAttribute()]
        public string SplitRule;

        [XmlAttribute()]
        public string ParamField;
        /// <summary>
        /// treeview
        /// </summary>
        [XmlAttribute()]
        public string DescField;

        /// <remarks/>
        [XmlAttribute()]
        public string Caption;
        /// <summary>
        /// 同义 Caption，和资源文件中的key对应，用于多语言切换
        /// </summary>
        [XmlAttribute()]
        public string ResourceKey;

        [XmlAttribute()]
        public string TrvSpName;

        [XmlAttribute()]
        public CtrlType CtrlType;

        [XmlAttribute()]
        public string DicCodeType;
        /// <summary>
        /// 如果CtrType=Form时，弹出的窗体所关联的ViewDatas名称
        /// </summary>
        [XmlAttribute()]
        public string TargetKey;

        [XmlAttribute()]
        public string VerifyReg;

        [XmlAttribute()]
        public string ValueType;
        /// <summary>
        /// 20150316 RadioButton 传值
        /// </summary>
        [XmlAttribute()]
        public string Value;

        [XmlAttribute()]
        public int Width = 200;
        //用于Lookup 的DropSize 
        [XmlAttribute()]
        public int DropWidth = 600;
        [XmlAttribute()]
        public int DropHeight = 400;
        /// <summary>
        /// 根据key值，调用获取默认值方法Common.GetDefaultQueryCondition(string key)
        /// 如果是日期控件，1 代表月初，
        /// 如果是RadioButton，Y 选中；N 未选中（默认）
        /// </summary>
        [XmlAttribute()]
        public string InitValue;
        [XmlAttribute()]
        public int Height;

        [XmlAttribute()]
        public int RowIndex = 1;

        [XmlAttribute()]
        public string Icon;

        [XmlAttribute()]
        public string ShortcutKeys;

        [XmlElement("BillExtArgs")]
        public BillExtArgs ExtArgs;
        /// <summary>
        /// 日期控件时，此值表示是否启用无效日期
        /// </summary>
        [XmlAttribute()]
        public bool NotNull { set; get; }

        [XmlAttribute()]
        public bool ReadOnly;
        /// <summary>
        /// 下拉列表时，是否包含扩展选项
        /// </summary>
        [XmlAttribute()]
        public bool HasExtendItem;
        /// <summary>
        /// 下拉列表包含扩展选项时，扩展选项显示的名称
        /// </summary>
        [XmlAttribute()]
        public string ExtendItemName;
    }

    public class BillExtArgs
    {
        [XmlAttribute()]
        public int Args = -1;

        [XmlAttribute()]
        public int MatchArgs = -1;

        [XmlAttribute()]
        public bool MultiCommit;

        [XmlAttribute()]
        public int CommitCount = 1;
    }
}
